package com.mengya.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.common.utils.http.Parameter;
import com.common.utils.http.SyncHttpClient;
import com.common.utils.mapper.JsonMapper;
import com.gzcwt.entity.QueryFileInfoResult;
import com.gzcwt.entity.Resource;
import com.mengya.gzcwt.R;
import com.mengya.utils.FileDBO;

public class UpdateActivity extends BaseActivity {
	private TextView info = null;
	private ProgressBar progreess = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.updateview);
		info = (TextView) findViewById(R.id.info);
		progreess = (ProgressBar) findViewById(R.id.progress);

		new AsyncQueryFilesInfo(this).execute("");

	}

	class AsyncQueryFilesInfo extends
			AsyncTask<String, String, QueryFileInfoResult> {
		private Context context;
		private FileDBO filedb = new FileDBO();
		private QueryFileInfoResult result = null;

		public AsyncQueryFilesInfo(Context c) {
			context = c;
		}

		protected QueryFileInfoResult doInBackground(String... arg0) {
			System.out.println("lance = hardno=" + hardinfo);
			String url = "http://113.10.242.75/gzcwt/QueryFileInfo.json";
			SyncHttpClient client = new SyncHttpClient();
			List<Parameter> paras = new ArrayList<Parameter>();
			paras.add(new Parameter("lastgettime", "2013-03-12 00:02:19"));
			paras.add(new Parameter("imei", hardinfo));
			String res = "";
			try {
				res = client.httpPost(url, paras);
			} catch (Exception e) {
				e.printStackTrace();
				result = null;
			}
			JsonMapper json = JsonMapper.buildNormalMapper();
			result = json.fromJson(res, QueryFileInfoResult.class);
			return result;
		}

		@Override
		protected void onPostExecute(QueryFileInfoResult rt) {
			super.onPostExecute(result);
			if (rt != null && rt.getReslist().size() > 0) {
				System.out.println("lance" + result.getCount());
				List<Resource> resourelist = rt.getReslist();
				if (null != resourelist && rt.getCount() > 0) {
					for (int i = 0; i < rt.getCount(); i++) {
						filedb.insertIntoFiles(resourelist.get(i));
					}
					progreess.setVisibility(View.GONE);
					info.setText("获取数据量" + rt.getCount() + "条");

					// file_id
					// TODO insert test insertIntoTests(Test);
				}
			} else {
				showToast("没有获取数据");
				progreess.setVisibility(View.GONE);
			}
		}
	}

	class AsyncQueryTestInfo extends
			AsyncTask<String, String, QueryFileInfoResult> {
		private Context context;
		private FileDBO filedb = new FileDBO();
		private QueryFileInfoResult result = null;

		public AsyncQueryTestInfo(Context c) {
			context = c;
		}

		protected QueryFileInfoResult doInBackground(String... arg0) {
			String url = "http://113.10.242.75/gzcwt/QueryFileInfo.json";
			SyncHttpClient client = new SyncHttpClient();
			List<Parameter> paras = new ArrayList<Parameter>();
			paras.add(new Parameter("lastgettime", "2013-03-12 00:02:19"));
			paras.add(new Parameter("imei", hardinfo));
			String res = "";
			try {
				res = client.httpPost(url, paras);
			} catch (Exception e) {
				e.printStackTrace();
				result = null;
			}
			JsonMapper json = JsonMapper.buildNormalMapper();
			result = json.fromJson(res, QueryFileInfoResult.class);
			return result;
		}

		@Override
		protected void onPostExecute(QueryFileInfoResult rt) {
			super.onPostExecute(result);
			if (rt != null && rt.getReslist().size() > 0) {
				System.out.println("lance" + result.getCount());
				List<Resource> resourelist = rt.getReslist();
				if (null != resourelist && rt.getCount() > 0) {
					for (int i = 0; i < rt.getCount(); i++) {
						filedb.insertIntoFiles(resourelist.get(i));
					}
					progreess.setVisibility(View.GONE);
					info.setText("获取数据量" + rt.getCount() + "条");

					// file_id
					// TODO insert test insertIntoTests(Test);
				}
			} else {
				showToast("没有获取数据");
				progreess.setVisibility(View.GONE);
			}
		}
	}

}
